package com.flexycore.iphone.view;



import com.flexycore.iphone.coregraphics.CGPoint;
import com.flexycore.iphone.coregraphics.CGRect;
import com.flexycore.iphone.model.Activity;
import com.flexycore.iphone.uikit.UIActivityIndicatorView;
import com.flexycore.iphone.uikit.UIActivityIndicatorViewStyle;
import com.flexycore.iphone.uikit.UIButton;
import com.flexycore.iphone.uikit.UIControlEvents;
import com.flexycore.iphone.uikit.UIImage;
import com.flexycore.iphone.uikit.UIView;

/**
 * 
 * @author matthieu nicolazo
 *
 */
public class ActivityIndicatorView extends UIView {
	/**
	 * Field
	 */
	private UIActivityIndicatorView activityIndicator;
	private UIButton button;
	
	/**
	 * Constructor
	 */
	public ActivityIndicatorView(){
		super();
		init();
		
		/*
		 *  "Play/pause" button
		 *  Be used to start or stop UIActivityIndicator animation.
		 */
		button = new UIButton(){
			//@Override
			public void controlEvent() {
				startActivity();
			}
		};
		button.initWithFrame(new CGRect().init(135, 100, 50, 50));
		button.setImageForState(UIImage.imageNamed("play.png"), 0);
		button.addTargetActionForControlEvents(UIControlEvents.UIControlEventTouchUpInside);
		addSubview(button);
		
		/*
		 *  Activity Indicator
		 */
		activityIndicator = new UIActivityIndicatorView();
		activityIndicator.initWithActivityIndicatorStyle(UIActivityIndicatorViewStyle.UIActivityIndicatorViewStyleWhiteLarge);
		activityIndicator.setCenter(new CGPoint().init(160, 50));
		activityIndicator.setHidesWhenStopped(false);
		addSubview(activityIndicator);
	}
	
	/**
	 * 
	 */
	public void startActivity(){
		button.setEnabled(false);
		activityIndicator.startAnimating();
		new Thread(new Activity(this)).start();
	}
	
	/**
	 * 
	 */
	public void stopActivity(){
		activityIndicator.stopAnimating();
		button.setEnabled(true);
	}
	
}
